# Import phone library
import appuifw
import e32
import calendar
import time

now = time.time()
minute = 60
hour = 60 * 60
week = 7 * 24 * 60 * 60

# Reduce typing for making popups
popUp = appuifw.note

# See last line
app_lock = e32.Ao_lock()

textArea = appuifw.Text()
appuifw.app.body = textArea

textArea.font = 'normal'
textArea.color = 0x000000 # BLACK

# Function declarations
def quit():
    app_lock.signal()

def showCalendar():
    popUp(u"to do")

def addEntry():
    cal = calendar.open()
    newEntry = cal.add_appointment() # Adds an entry to calendar
    newEntry.content = appuifw.query(u"Enter description: ", "text")
    newEntry.location = appuifw.query(u"Enter location: ", "text")
    entryDateStart = appuifw.query(u"Enter date: ", "date")
    entryTimeStart = appuifw.query(u"Enter start time: ", "time")
    entryTimeEnd = appuifw.query(u"Enter end time: ", "time")
    popUp(unicode(entryTimeStart) + u" " + unicode(entryTimeEnd))
    textArea.add(unicode(entryTimeStart) + u" " + unicode(entryTimeEnd))
    entryAlarm = appuifw.query(u"Alarm?", "query")
    #startOfDay = now - time.time()
    #textArea.add(unicode(startOfDay))
    newEntry.set_time(entryTimeStart + int(entryDateStart), entryTimeEnd + int(entryDateStart))
    newEntry.alarm = now + 1*minute
    newEntry.commit() # Updates database

def displayEntries():
    cal = calendar.open()
    for entries in cal:
        # textArea.add could be put into another function that automatically
        # converts to unicode and adds newline
        event = cal[entries]
        textArea.add(u"ID: " + unicode(event.id) + "\n")
        textArea.add(u"Content: " + unicode(event.content) + "\n")
        textArea.add(u"Originating: " + unicode(event.originating) + "\n")
        textArea.add(u"Location: " + unicode(event.location) + "\n")
        textArea.add(u"Start: " + unicode(time.ctime(event.start_time)) + "\n")
        textArea.add(u"End: " + unicode(time.ctime(event.end_time)) + "\n")
        textArea.add(u"Recurring: ")
        # This line I got off http://code.activestate.com/recipes/52282/ from Hamish Lawson
        # Similiar to the cond ? val1 : val2 from c++ and java it is a one line if
        # then else statement, thus,
        # if get_repeat is none (python equivalent of null)
        #   Print no
        # else
        #   Print yes
        textArea.add(unicode((event.get_repeat() == None and [u"No"] or [u"Yes"])[0]) + "\n")
        textArea.add(u"\n\n")
    textArea.add(u"Number of entries: " + unicode(len(cal)))

# This line adds a list of options to the button on the top left
# Menu must take format [(u"Option1", functionToRun), (u"Option2", function2)]
# u preceding "text" indicates unicode
appuifw.app.menu = [(u"Add Entry", addEntry),
                    (u"Display Entries", displayEntries),
                    (u"Exit", quit)]

# This line adds functionality to the top right button
appuifw.app.exit_key_handler = quit

# This prevents the application from exiting straightaway
# NOTE: You can't have e32.Ao_lock().wait() so e32.Ao_lock() is changed into
#       app_lock
app_lock.wait()
